package com.firefighting.maintenancer;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.firefighting.entity.Contract;
import com.firefighting.entity.FirstMaintenanceProject;
import com.firefighting.util.JdbcUtils;
import com.firefighting.util.MYJSONObject;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class CheckPlanDetail
 */
@WebServlet(asyncSupported = true, urlPatterns = { "/CheckPlanDetail" })
public class CheckTaskDetail extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		System.out.println("-----"+format.format(new Date())+"    "+this.getServletName()+"-----");
		

		String userName=request.getParameter("userName");
		//String maintenanceplan_id=request.getParameter("maintenanceplan_id");
		String task_id=request.getParameter("task_id");
		PrintWriter out = response.getWriter();
		MYJSONObject result_jb = new MYJSONObject();
		
		Connection connection = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String proprietor_photo="";
	//	ResultSetMetaData data =null;
		try {
			connection = JdbcUtils.getConnection();
			String sql = "select proprietor_photo from task where task.id=? ";
			ps = connection.prepareStatement(sql);
			ps.setString(1, task_id);
			rs = ps.executeQuery();
			while(rs.next())
			{
				proprietor_photo=rs.getString(1);
			}
			ps.close();
			rs.close();
			 sql = "select fmp.Id,fmp.Name " 
					+ "from first_maintenance_project fmp,second_maintenance_project smp,users u,maintenance_test_content mtc,task t,task_item d " 
					+ "where u.userName=? and t.id=? and t.id=d.task_id and " 
					+ "d.maintenance_detail_id=mtc.Id and mtc.Father_maintenance_id=smp.Id and smp.father_id=fmp.Id " 
					+ "group by fmp.Id,fmp.Name";
					ps = connection.prepareStatement(sql);
					ps.setString(1, userName);
					ps.setString(2, task_id);
					rs = ps.executeQuery();
					ArrayList<FirstMaintenanceProject> firstMaintenanceProject_array_f = new ArrayList<>();
					while(rs.next())
					{
						FirstMaintenanceProject fristMaintenanceProject_f = new FirstMaintenanceProject();
						fristMaintenanceProject_f.setId(rs.getInt(1));
						fristMaintenanceProject_f.setName(rs.getString(2));
						firstMaintenanceProject_array_f.add(fristMaintenanceProject_f);
					}
					JSONArray firstMaintenanceProject_ja_f = new JSONArray();
					for (FirstMaintenanceProject fristMaintenanceProject : firstMaintenanceProject_array_f) {
						MYJSONObject firstMaintenanceProject_jb_f = new MYJSONObject();
						firstMaintenanceProject_jb_f.put("first_project_id", fristMaintenanceProject.getId());
						firstMaintenanceProject_jb_f.put("first_project_name", fristMaintenanceProject.getName());
						JSONArray secondMaintenanceProject_ja_f = new JSONArray();
						System.out.println("11111111111  "+fristMaintenanceProject.getId());
						ps.close();
						rs.close();
						sql = "select smp.Id,smp.name " +
								"from first_maintenance_project fmp,second_maintenance_project smp,users u,maintenance_test_content mtc,task t,task_item d  " + 
								"where u.userName=? and t.id=? and fmp.Id=? and t.id=d.task_id and smp.father_id=fmp.Id " + 
								"and mtc.Father_maintenance_id=smp.Id and d.maintenance_detail_id=mtc.Id " + 
								"group by smp.Id,smp.name";
						ps = connection.prepareStatement(sql);
						ps.setString(1, userName);
						ps.setString(2,task_id);
						ps.setInt(3,fristMaintenanceProject.getId());
						rs = ps.executeQuery();
						while (rs.next()) {
							MYJSONObject secondMaintenanceProject_jb_f=new MYJSONObject();
							secondMaintenanceProject_jb_f.put("second_project_id", rs.getInt(1));
							secondMaintenanceProject_jb_f.put("second_project_name", rs.getString(2));
							secondMaintenanceProject_ja_f.add(secondMaintenanceProject_jb_f.toString());
						}
						firstMaintenanceProject_jb_f.put("second_project", secondMaintenanceProject_ja_f);
						firstMaintenanceProject_ja_f.add(firstMaintenanceProject_jb_f.toString());
					}
					ps.close();
					rs.close();
					result_jb.put("status","true");
					result_jb.put("proprietor_photo",proprietor_photo);
					result_jb.put("task_details", firstMaintenanceProject_ja_f);
			/*String sql = "select fmp.Id,fmp.Name " 
			+ "from first_maintenance_project fmp,second_maintenance_project smp,maintenanceplan_content mc,maintenanceplan mp,users u,maintenance_test_content mtc,task t " 
			+ "where u.userName=? and mp.id=? and mc.state_finished='已完成' and " 
			+ "smp.father_id=fmp.Id and u.userId=t.app_id_account and t.maintenanceplan_id=mp.id and mc.maintenanceplan_id=mp.id and mtc.Id=mc.maintenance_content_id and mtc.Father_maintenance_id=smp.id " 
			+ "group by fmp.Id,fmp.Name";
			ps = connection.prepareStatement(sql);
			ps.setString(1, userName);
			ps.setString(2, maintenanceplan_id);
			rs = ps.executeQuery();
			// data=rs.getMetaData();
		//	System.out.print(data.getColumnCount());
			// System.out.print(data.getColumnName(1));
			
			ArrayList<FirstMaintenanceProject> firstMaintenanceProject_array_f = new ArrayList<>();
			while(rs.next())
			{
				FirstMaintenanceProject fristMaintenanceProject_f = new FirstMaintenanceProject();
				fristMaintenanceProject_f.setId(rs.getInt(1));
				fristMaintenanceProject_f.setName(rs.getString(2));
				firstMaintenanceProject_array_f.add(fristMaintenanceProject_f);
			}
			JSONArray firstMaintenanceProject_ja_f = new JSONArray();
			for (FirstMaintenanceProject fristMaintenanceProject : firstMaintenanceProject_array_f) {
				MYJSONObject firstMaintenanceProject_jb_f = new MYJSONObject();
				firstMaintenanceProject_jb_f.put("first_project_id", fristMaintenanceProject.getId());
				firstMaintenanceProject_jb_f.put("first_project_name", fristMaintenanceProject.getName());
				JSONArray secondMaintenanceProject_ja_f = new JSONArray();
				
				ps.close();
				rs.close();
				sql = "select smp.id,smp.name"
						+ " from first_maintenance_project fmp,second_maintenance_project smp,maintenanceplan_content mc,maintenanceplan mp,users u,maintenance_test_content mtc,task t "
						+ "where u.userName=? and mp.id=? and fmp.Id=? and smp.father_id=fmp.Id and u.userId=t.app_id_account and t.maintenanceplan_id=mp.id "
						+ "and mc.maintenanceplan_id=mp.id and mtc.Id=mc.maintenance_content_id and mtc.Father_maintenance_id=smp.id "
						+ "and u.userId=t.app_id_account and mc.state_finished='已完成' "
						+ "group by smp.id,smp.name";
				ps = connection.prepareStatement(sql);
				ps.setString(1, userName);
				ps.setString(2,maintenanceplan_id);
				ps.setInt(3,fristMaintenanceProject.getId());
				rs = ps.executeQuery();
				while (rs.next()) {
					MYJSONObject secondMaintenanceProject_jb_f=new MYJSONObject();
					secondMaintenanceProject_jb_f.put("second_project_id", rs.getInt(1));
					secondMaintenanceProject_jb_f.put("second_project_name", rs.getString(2));
					secondMaintenanceProject_ja_f.add(secondMaintenanceProject_jb_f.toString());
				}
				firstMaintenanceProject_jb_f.put("second_project", secondMaintenanceProject_ja_f);
				firstMaintenanceProject_ja_f.add(firstMaintenanceProject_jb_f.toString());
			}
			ps.close();
			rs.close();
		    sql = "select fmp.Id,fmp.Name " 
					+ "from first_maintenance_project fmp,second_maintenance_project smp,maintenanceplan_content mc,maintenanceplan mp,users u,maintenance_test_content mtc,task t "
					+ "where u.userName=? and mp.id=? and mc.state_finished='未完成' and " 
					+ "smp.father_id=fmp.Id and u.userId=t.app_id_account and t.maintenanceplan_id=mp.id and mc.maintenanceplan_id=mp.id and mtc.Id=mc.maintenance_content_id and mtc.Father_maintenance_id=smp.id " 
					+ "group by fmp.Id,fmp.Name";
			ps = connection.prepareStatement(sql);
			ps.setString(1, userName);
			ps.setString(2, maintenanceplan_id);
			rs = ps.executeQuery();
			ArrayList<FirstMaintenanceProject> firstMaintenanceProject_array_u = new ArrayList<>();
			while(rs.next())
			{
				FirstMaintenanceProject fristMaintenanceProject_u = new FirstMaintenanceProject();
				fristMaintenanceProject_u.setId(rs.getInt(1));
				fristMaintenanceProject_u.setName(rs.getString(2));
				firstMaintenanceProject_array_u.add(fristMaintenanceProject_u);
			}
			JSONArray firstMaintenanceProject_ja_u = new JSONArray();
			for (FirstMaintenanceProject fristMaintenanceProject : firstMaintenanceProject_array_u) {
				MYJSONObject firstMaintenanceProject_jb_u = new MYJSONObject();
				firstMaintenanceProject_jb_u.put("first_project_id", fristMaintenanceProject.getId());
				firstMaintenanceProject_jb_u.put("first_project_name", fristMaintenanceProject.getName());
				JSONArray secondMaintenanceProject_ja_u= new JSONArray();
				
				ps.close();
				rs.close();
				sql =  "select smp.id,smp.name"
						+ " from first_maintenance_project fmp,second_maintenance_project smp,maintenanceplan_content mc,maintenanceplan mp,users u,maintenance_test_content mtc,task t "
						+ "where u.userName=? and mp.id=? and fmp.Id=? and smp.father_id=fmp.Id and u.userId=t.app_id_account and t.maintenanceplan_id=mp.id "
						+ "and mc.maintenanceplan_id=mp.id and mtc.Id=mc.maintenance_content_id and mtc.Father_maintenance_id=smp.id "
						+ "and u.userId=t.app_id_account and mc.state_finished='未完成' "
						+ "group by smp.id,smp.name";
				ps = connection.prepareStatement(sql);
				ps.setString(1, userName);
				ps.setString(2,maintenanceplan_id);
				ps.setInt(3,fristMaintenanceProject.getId());
				rs = ps.executeQuery();
				while (rs.next()) {
					MYJSONObject secondMaintenanceProject_jb_u=new MYJSONObject();
					secondMaintenanceProject_jb_u.put("second_project_id", rs.getInt(1));
					secondMaintenanceProject_jb_u.put("second_project_name", rs.getString(2));
					secondMaintenanceProject_ja_u.add(secondMaintenanceProject_jb_u.toString());
				}
				firstMaintenanceProject_jb_u.put("second_project", secondMaintenanceProject_ja_u);
				firstMaintenanceProject_ja_u.add(firstMaintenanceProject_jb_u.toString());
			}

			result_jb.put("status","true");
			result_jb.put("finished", firstMaintenanceProject_ja_f);
			result_jb.put("unfinished", firstMaintenanceProject_ja_u);
			System.out.println(result_jb.toString());*/
		} catch (SQLException e) {
			e.printStackTrace();
			result_jb.put("status", "false");
			result_jb.put("message", "服务器异常");
		} finally {
			JdbcUtils.free(rs, ps, connection);
			out.print(result_jb.toString());
			out.flush();
			out.close();
		}
	}

}
